package com.jsh.mypor.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jsh.mypor.pojo.Clazz;
import com.jsh.mypor.pojo.Grade;
import com.jsh.mypor.service.GradeService;
import com.jsh.mypor.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
@Api("年级管理控制器")
@RestController
@RequestMapping("/sms/gradeController")
public class GradeController {

    @Autowired
    private GradeService gradeService;
    @GetMapping("/getGrades")
    public Result getGrades(  ){
        List<Grade> list = gradeService.getClazzsByOpr();
        return Result.ok(list);
    }

    //sms/gradeController/getGrades/1/3?gradeName
    @ApiOperation("查询方法")
    @GetMapping("/getGrades/{pageNo}/{pageSize}")
    public Result getGrades(
            @ApiParam("分页查询的页数") @PathVariable("pageNo") Integer pageNo,
            @ApiParam("每页有多少条数据") @PathVariable("pageSize") Integer pageSize,
            String gradeName
    ){
        //分页 带条件查询
        Page<Grade> page=new Page<>(pageNo,pageSize);
        //通过服务层
        IPage<Grade> pageRs=gradeService.getGradeByOpr(page,gradeName);
        //封装Result对象并返回
        return Result.ok(pageRs);
    }
    @ApiOperation("新增或者修改方法")
    @PostMapping("/saveOrUpdateGrade")
    public Result<Grade>  saveOrUpdateGrade(
            @ApiParam("年级信息")  @RequestBody Grade grade
    ){
        boolean b = gradeService.saveOrUpdate(grade);
     if (!b){
         return Result.fail();
     }

        return Result.ok();
    }
    @ApiOperation("删除的方法")
    @DeleteMapping("/deleteGrade")
    public Result<Grade> deleteGrade(
            @ApiParam("要删除信息的ids")@RequestBody List<Integer> nums){
        boolean b = gradeService.removeByIds(nums);

        if (!b){
            return Result.fail();
        }

        return Result.ok();
    }


}
